package com.css.dzj.zfjg.modules.taskinfo.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.fb.entity.JgFbLhjgEntity;
import com.css.dzj.zfjg.modules.taskinfo.entity.JgLhjgRwsEntity;
import com.css.dzj.zfjg.modules.taskinfo.service.TaskInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 联合监管任务书controller
 */
@RestController
@RequestMapping("/taskinfo")
public class TaskInfoController {

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private BaseService baseService;


    /**
     * 添加 任务书
     * @param jgLhjgRwsEntity
     * @return
     */
    @PostMapping("/add")
    public R addTaskInfo(@RequestBody JgLhjgRwsEntity jgLhjgRwsEntity, HttpServletRequest request) {
        try {
            if(StringUtils.isEmpty(jgLhjgRwsEntity)) {
                return R.error("任务书不能为空");
            }
            taskInfoService.add(jgLhjgRwsEntity, request);
            baseService.logSuccess(3, "单个任务书添加成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "单个任务书添加失败", "2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 更新任务书
     * @param jgLhjgRwsEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody JgLhjgRwsEntity jgLhjgRwsEntity) {
        try {
            if(StringUtils.isEmpty(jgLhjgRwsEntity)) {
                return R.error("任务书不能为空");
            }
            JgLhjgRwsEntity oldJgTaskinfoEntity = taskInfoService.findById(jgLhjgRwsEntity.getTaskId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgRwsEntity, oldJgTaskinfoEntity);
            jgLhjgRwsEntity.setUpdateTime(new Date());
            taskInfoService.update(jgLhjgRwsEntity);
            baseService.logSuccess(3, "单个任务书修改成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "单个任务书修改失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过id查询任务书
     * @param id
     * @return
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam String id) {
        try {
            JgLhjgRwsEntity jgLhjgRwsEntity = taskInfoService.findById(id);
            baseService.logSuccess(3, "通过id获取单个任务书", "2");
            return R.ok().put("data", jgLhjgRwsEntity);
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取单个任务书", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询所有任务书
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/queryAll")
    public R queryAll(@RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgRwsEntity> taskInfoQueryResults = taskInfoService.queryAll(pageRequest);
            baseService.logSuccess(3, "分页查询单个任务书", "2");
            return R.ok().put("data",taskInfoQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询单个任务书", "2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 多条件分页查询任务书
     * @param pageSize
     * @param pageNo
     * @param status
     * @return
     */
    @GetMapping("listTaskInfo")
    public R listClue(@RequestParam Integer pageSize, @RequestParam Integer pageNo,
                      @RequestParam String taskName, @RequestParam String superviseObjectType,
                      @RequestParam String superviseObjectName,
                      @RequestParam String taskStartTimeStart,
                      @RequestParam String taskStartTimeEnd,
                      @RequestParam String status) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgRwsEntity> taskInfoQueryResults = taskInfoService.listTaskInfo(pageRequest, taskName, superviseObjectType, superviseObjectName,taskStartTimeStart,taskStartTimeEnd, status);
            baseService.logSuccess(3, "多条件分页查询单个任务书", "2");
            return R.ok().put("data", taskInfoQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "多条件分页查询单个任务书", "2");
            return R.error(e.getMessage());
        }
    }
    /**
     * 分发 任务
     * @param jgFbLhjgEntity
     * @return
     */
    @PostMapping("/fenfa")
    public R fenfaTaskInfo(@RequestBody JgFbLhjgEntity jgFbLhjgEntity, HttpServletRequest request) {
        try {
            if(StringUtils.isEmpty(jgFbLhjgEntity)) {
                return R.error("分发内容不能为空");
            }
            taskInfoService.fenfa(jgFbLhjgEntity, request);
            baseService.logSuccess(3, "任务分发", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "任务分发", "2");
            return R.error(e.getMessage());
        }

    }

}
